<?php

class Model_soal extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->path = realpath(APPPATH . '../gambarsoal');
        $this->load->helper('date');
        $this->load->helper('string');
        $this->load->library('pagination');
    }

    function getSoal($id) {
        $query = $this->db->query('SELECT s.id, g.nama, s.kelas, s.materi, t.type_soal, s.jumlah_soal, s.jumlah_opsi, s.mode_acak, s.waktu FROM soal s INNER JOIN guru g ON s.id_guru = g.id INNER JOIN type_soal t ON s.id_type_soal = t.id WHERE s.id_guru = ' . $id . ' AND s.is_deleted=0');

        return $query->result();
    }
    
    function getSoalAdmin(){
        $query = $this->db->query('SELECT s.id, g.nama, s.kelas, s.materi, t.type_soal, s.jumlah_soal, s.jumlah_opsi, s.mode_acak, s.waktu FROM soal s INNER JOIN guru g ON s.id_guru = g.id INNER JOIN type_soal t ON s.id_type_soal = t.id WHERE s.is_deleted=0');

        return $query->result();
    }

    function getKerjakanSoal($id, $perpage, $segment) {
        if ($segment == '') {
            $segment = 0;
        }
        $ljt = $segment;
        $session = $this->session->userdata('id');
        $query = $this->db->query("SELECT * FROM detail_soal WHERE id_soal = $id ORDER BY rand($session) LIMIT $segment,1");
        return $query->row();
    }

    function getSoalSiswa($kelas) {
        $query = $this->db->query("SELECT s.id, g.nama, s.kelas, s.materi, t.type_soal, s.jumlah_soal, s.jumlah_opsi, s.mode_acak, s.waktu FROM soal s INNER JOIN guru g ON s.id_guru = g.id INNER JOIN type_soal t ON s.id_type_soal = t.id WHERE s.kelas LIKE '%$kelas%' AND s.is_deleted=0");

        return $query->result();
    }

    function getTypeSoal() {
        $query1 = $this->db->get('type_soal');

        return $query1->result();
    }

    function getEditSoal($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('soal');

        return $query->row_array();
    }

    function getDetailSoal($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('detail_soal');

        return $query->row_array();
    }

    function soalsave() {
        $id_guru = $this->input->post('id_guru');
        $kelas = $this->input->post('kelas');
        $materi = $this->input->post('materi');
        $type_soal = $this->input->post('type_soal');
        $jml_soal = $this->input->post('jml_soal');
        $jml_opsi = $this->input->post('jml_opsi');
        $mode_acak = $this->input->post('mode_acak');
        $waktu = $this->input->post('waktu');
        $data = array(
            'id_guru' => $id_guru,
            'kelas' => $kelas,
            'materi' => $materi,
            'id_type_soal' => $type_soal,
            'jumlah_soal' => $jml_soal,
            'jumlah_opsi' => $jml_opsi,
            'mode_acak' => $mode_acak,
            'waktu' => $waktu
        );

        $this->db->insert('soal', $data);
        $id = $this->db->insert_id();
        $this->session->set_userdata('id_soal', $id);
        $this->session->set_userdata('opsi', $jml_opsi);
    }

    function updateSoal() {
        $id = $this->input->post('id');
        $kelas = $this->input->post('kelas');
        $materi = $this->input->post('materi');
        $type_soal = $this->input->post('type_soal');
        $jml_soal = $this->input->post('jml_soal');
        $jml_opsi = $this->input->post('jml_opsi');
        $mode_acak = $this->input->post('mode_acak');
        $waktu = $this->input->post('waktu');
        $data = array(
            'kelas' => $kelas,
            'materi' => $materi,
            'id_type_soal' => $type_soal,
            'jumlah_soal' => $jml_soal,
            'jumlah_opsi' => $jml_opsi,
            'mode_acak' => $mode_acak,
            'waktu' => $waktu
        );
        $this->db->where('id', $id);
        $this->db->update('soal', $data);
    }

    function countdetsoal() {
        $id_soal = $this->session->userdata('id_soal');
        $q1 = $this->db->query("SELECT jumlah_soal FROM soal WHERE id = $id_soal");
        $row = $q1->row();
        $qtysoal = $row->jumlah_soal;

        $q2 = $this->db->query("SELECT * FROM detail_soal WHERE id_soal = $id_soal");
        $curqtysoal = $q2->num_rows();
        if ($curqtysoal >= $qtysoal) {
            $data = array(
                'end' => true
            );
        } else {
            $data = array(
                'end' => false,
                'no_soal' => $curqtysoal + 1
            );
        }
        return $data;
    }

    function countjumsoal($id) {
        $q1 = $this->db->query("SELECT jumlah_soal, jumlah_opsi FROM soal WHERE id = $id");
        return $q1->row();
    }

    function itungdetsoal($id) {
        $this->db->where('id_soal', $id);
        $itung = $this->db->get('detail_soal');

        return $itung->num_rows();
    }

    function retrievedetsoal($id) {
        $query = $this->db->query("SELECT * FROM detail_soal WHERE id_soal = $id");
        return $query->result();
    }

    function insertdetsoal() {
        $date = now();
//        text
        if ($this->input->post('pertanyaan') == '') {
            $pertanyaan = null;
        } else {
            $pertanyaan = $this->input->post('pertanyaan');
        }

        if ($this->input->post('opsi1') == '') {
            $opsi1 = null;
        } else {
            $opsi1 = $this->input->post('opsi1');
        }

        if ($this->input->post('opsi2') == '') {
            $opsi2 = null;
        } else {
            $opsi2 = $this->input->post('opsi2');
        }

        if ($this->input->post('opsi3') == '') {
            $opsi3 = null;
        } else {
            $opsi3 = $this->input->post('opsi3');
        }

        if ($this->input->post('opsi4') == '') {
            $opsi4 = null;
        } else {
            $opsi4 = $this->input->post('opsi4');
        }

        if ($this->input->post('opsi5') == '') {
            $opsi5 = null;
        } else {
            $opsi5 = $this->input->post('opsi5');
        }

//        pic
        if ($_FILES["pertanyaanpic"]["name"] == '' || !isset($_FILES["pertanyaanpic"])) {
            $gambarsoal = null;
        } else {
            $rand = random_string('alnum', 32);
            $pertanyaanpic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $pertanyaanpic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('pertanyaanpic');
            $name = $_FILES["pertanyaanpic"]["name"];
            $ext = end(explode(".", $name));
            $gambarsoal = $pertanyaanpic . '.' . $ext;
        }

        if ($_FILES["opsi1pic"]["name"] == '' || !isset($_FILES["opsi1pic"])) {
            $gambara = null;
        } else {
            $rand = random_string('alnum', 32);
            $opsi1pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi1pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi1pic');
            $name = $_FILES["opsi1pic"]["name"];
            $ext1 = end(explode(".", $name));
            $gambara = $opsi1pic . '.' . $ext1;
        }

        if ($_FILES["opsi2pic"]["name"] == '' || !isset($_FILES["opsi2pic"])) {
            $gambarb = null;
        } else {
            $rand = random_string('alnum', 32);
            $opsi2pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi2pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi2pic');
            $name = $_FILES["opsi2pic"]["name"];
            $ext2 = end(explode(".", $name));
            $gambarb = $opsi2pic . '.' . $ext2;
        }

        if ($_FILES["opsi3pic"]["name"] == '' || !isset($_FILES["opsi3pic"])) {
            $gambarc = null;
        } else {
            $rand = random_string('alnum', 32);
            $opsi3pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi3pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi3pic');
            $name = $_FILES["opsi3pic"]["name"];
            $ext3 = end(explode(".", $name));
            $gambarc = $opsi3pic . '.' . $ext3;
        }

        if (!isset($_FILES['opsi4pic']) || $_FILES["opsi4pic"]["name"] == '') {
            $gambard = null;
        } else {
            $rand = random_string('alnum', 32);
            $opsi4pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi4pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi4pic');
            $name = $_FILES["opsi4pic"]["name"];
            $ext4 = end(explode(".", $name));
            $gambard = $opsi4pic . '.' . $ext4;
        }

        if (!isset($_FILES['opsi5pic']) || $_FILES["opsi5pic"]["name"] == '') {
            $gambare = null;
        } else {
            $rand = random_string('alnum', 32);
            $opsi5pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi5pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi5pic');
            $name = $_FILES["opsi5pic"]["name"];
            $ext5 = end(explode(".", $name));
            $gambare = $opsi5pic . '.' . $ext5;
        }

        $kunci = $this->input->post('kunci');

        $data = array(
            'id_soal' => $this->session->userdata('id_soal'),
            'soal' => $pertanyaan,
            'gambarsoal' => $gambarsoal,
            'opsia' => $opsi1,
            'gambara' => $gambara,
            'opsib' => $opsi2,
            'gambarb' => $gambarb,
            'opsic' => $opsi3,
            'gambarc' => $gambarc,
            'opsid' => $opsi4,
            'gambard' => $gambard,
            'opsie' => $opsi5,
            'gambare' => $gambare,
            'kunci' => $kunci
        );

        $this->db->insert('detail_soal', $data);
    }

    function editdetsoal() {
        $date = now();
//        text
        if ($this->input->post('pertanyaan') == '') {
            $pertanyaan = null;
        } else {
            $pertanyaan = $this->input->post('pertanyaan');
        }

        if ($this->input->post('opsi1') == '') {
            $opsi1 = null;
        } else {
            $opsi1 = $this->input->post('opsi1');
        }

        if ($this->input->post('opsi2') == '') {
            $opsi2 = null;
        } else {
            $opsi2 = $this->input->post('opsi2');
        }

        if ($this->input->post('opsi3') == '') {
            $opsi3 = null;
        } else {
            $opsi3 = $this->input->post('opsi3');
        }

        if ($this->input->post('opsi4') == '') {
            $opsi4 = null;
        } else {
            $opsi4 = $this->input->post('opsi4');
        }

        if ($this->input->post('opsi5') == '') {
            $opsi5 = null;
        } else {
            $opsi5 = $this->input->post('opsi5');
        }

//        pic
        if ($_FILES["pertanyaanpic"]["name"] == '' || !isset($_FILES["pertanyaanpic"])) {
            $gambarsoal = $this->input->post('pertanyaanpichid');
        } else {
            $rand = random_string('alnum', 32);
            $pertanyaanpic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $pertanyaanpic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('pertanyaanpic');
            $name = $_FILES["pertanyaanpic"]["name"];
            $ext = end(explode(".", $name));
            $gambarsoal = $pertanyaanpic . '.' . $ext;
        }

        if ($_FILES["opsi1pic"]["name"] == '' || !isset($_FILES["opsi1pic"])) {
            $gambara = $this->input->post('opsi1pichid');
        } else {
            $rand = random_string('alnum', 32);
            $opsi1pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi1pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi1pic');
            $name = $_FILES["opsi1pic"]["name"];
            $ext1 = end(explode(".", $name));
            $gambara = $opsi1pic . '.' . $ext1;
        }

        if ($_FILES["opsi2pic"]["name"] == '' || !isset($_FILES["opsi2pic"])) {
            $gambarb = $this->input->post('opsi2pichid');
        } else {
            $rand = random_string('alnum', 32);
            $opsi2pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi2pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi2pic');
            $name = $_FILES["opsi2pic"]["name"];
            $ext2 = end(explode(".", $name));
            $gambarb = $opsi2pic . '.' . $ext2;
        }

        if ($_FILES["opsi3pic"]["name"] == '' || !isset($_FILES["opsi3pic"])) {
            $gambarc = $this->input->post('opsi3pichid');
        } else {
            $rand = random_string('alnum', 32);
            $opsi3pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi3pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi3pic');
            $name = $_FILES["opsi3pic"]["name"];
            $ext3 = end(explode(".", $name));
            $gambarc = $opsi3pic . '.' . $ext3;
        }

        if (!isset($_FILES['opsi4pic']) || $_FILES["opsi4pic"]["name"] == '') {
            $gambard = $this->input->post('opsi4pichid');
        } else {
            $rand = random_string('alnum', 32);
            $opsi4pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi4pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi4pic');
            $name = $_FILES["opsi4pic"]["name"];
            $ext4 = end(explode(".", $name));
            $gambard = $opsi4pic . '.' . $ext4;
        }

        if (!isset($_FILES['opsi5pic']) || $_FILES["opsi5pic"]["name"] == '') {
            $gambare = $this->input->post('opsi5pichid');
        } else {
            $rand = random_string('alnum', 32);
            $opsi5pic = $date . $rand;
            $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                'upload_path' => $this->path,
                'file_name' => $opsi5pic
            );
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $upload = $this->upload->do_upload('opsi5pic');
            $name = $_FILES["opsi5pic"]["name"];
            $ext5 = end(explode(".", $name));
            $gambare = $opsi5pic . '.' . $ext5;
        }

        $kunci = $this->input->post('kunci');

        $data = array(
            'id_soal' => $this->session->userdata('id_soal'),
            'soal' => $pertanyaan,
            'gambarsoal' => $gambarsoal,
            'opsia' => $opsi1,
            'gambara' => $gambara,
            'opsib' => $opsi2,
            'gambarb' => $gambarb,
            'opsic' => $opsi3,
            'gambarc' => $gambarc,
            'opsid' => $opsi4,
            'gambard' => $gambard,
            'opsie' => $opsi5,
            'gambare' => $gambare,
            'kunci' => $kunci
        );

        $this->db->where('id', $this->input->post('id'));
        $this->db->update('detail_soal', $data);
        redirect('guru/detsoallaunch/' . $this->session->userdata('id_soal'));
    }

    function cekjawab($id) {
        $data = array(
            'id_detail_soal' => $id,
            'id_siswa' => $this->session->userdata('id')
        );
        $this->db->select('*');
        $this->db->from('hasil');
        $this->db->where($data);
        return $this->db->get()->row();
    }

    function jawab() {
        $id_soal = $this->input->post('id_soal');
        $id_det_soal = $this->input->post('id_det_soal');
        $id_siswa = $this->input->post('id_siswa');
        $jawaban = $this->input->post('jawaban');

        $data = array(
            'id_soal' => $id_soal,
            'id_detail_soal' => $id_det_soal,
            'id_siswa' => $id_siswa,
            'jawaban' => $jawaban
        );

        $this->db->insert('hasil', $data);
    }

    function updateJawab() {
        $id_soal = $this->input->post('id_soal');
        $id_det_soal = $this->input->post('id_det_soal');
        $id_siswa = $this->input->post('id_siswa');
        $jawaban = $this->input->post('jawaban');
        $id_jawab = $this->input->post('id_jawab');

        $data = array(
            'id_soal' => $id_soal,
            'id_detail_soal' => $id_det_soal,
            'id_siswa' => $id_siswa,
            'jawaban' => $jawaban
        );
        $this->db->where('id', $id_jawab);
        $this->db->update('hasil', $data);
    }

    function cek($id_soal, $session) {
        $point = $this->db->query("SELECT d.id, h.id_detail_soal,d.kunci, h.jawaban from detail_soal d INNER JOIN hasil h ON d.id = h.id_detail_soal WHERE h.id_soal = $id_soal AND h.id_siswa = $session ORDER BY d.id ");
        return $point->result();
    }

    function point($id_soal) {
        $point = $this->db->query("SELECT 100/jumlah_soal AS point FROM soal WHERE id=$id_soal");
        return $point->row();
    }
    
    function cekSelesai($id_soal) {
        $id_siswa = $this->session->userdata('id');
        $cek = $this->db->query("SELECT is_selesai FROM nilai WHERE id_soal=$id_soal AND id_siswa = $id_siswa");
        return $cek;
    }
    
    function getWaktu($id_soal){
        $id_siswa = $this->session->userdata('id');
        $waktu = $this->db->query("SELECT TIMESTAMPDIFF(SECOND,NOW(),batas_waktu) AS waktu FROM nilai WHERE id_soal=$id_soal AND id_siswa=$id_siswa");
        return $waktu->row();
    }
    
    function setNilai($id_soal) {
        $id_siswa = $this->session->userdata('id');
        $set = $this->db->query("INSERT INTO nilai (tgl_pengerjaan,batas_waktu,id_soal,id_siswa) VALUES(NOW(),(SELECT NOW() + INTERVAL (SELECT waktu FROM soal WHERE id=$id_soal) MINUTE),$id_soal,$id_siswa)");
    }
    
    function setHasil($id_soal) {
        $id_siswa = $this->session->userdata('id');
        $set = $this->db->query("INSERT INTO hasil (id_detail_soal,id_siswa, id_soal, jawaban) SELECT id,$id_siswa,$id_soal,0 from detail_soal where id_soal=$id_soal");
    }
    
    function simpanNilai($id_soal, $id_siswa, $betul, $salah, $nilai) {
        $data = array(
            'benar' => $betul,
            'salah' => $salah,
            'nilai' => $nilai,
            'is_selesai' => '1'
        );
        $where = array(
            'id_soal' => $id_soal,
            'id_siswa' => $id_siswa
        );
        $this->db->where($where);
        $query = $this->db->update('nilai', $data);
        return $query;
        //redirect('siswa/nilai/');
        
    }

}

